package com.example.teamtribe.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;

import com.chaychan.library.BottomBarLayout;
import com.example.teamtribe.R;
import com.example.teamtribe.UpdateService;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.event.Unread;
import com.example.teamtribe.utils.Utility;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class HomeActivity extends BaseActivity {

    private BottomBarLayout barLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ViewPager viewPager = findViewById(R.id.pagers_home);
        barLayout = findViewById(R.id.bottom_bar);
        viewPager.setAdapter(new HomeAdapter(getSupportFragmentManager()));
        barLayout.setViewPager(viewPager);

        Utility.setLogged(true);
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("HELLO_FINISH"));
        startService(new Intent(HomeActivity.this, UpdateService.class));
        EventBus.getDefault().register(this);
        new FakeData().start();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setUnread(Unread unread) {
        barLayout.setUnread(unread.getPosition(), unread.getNum());
    }
}
